package com.sntk.organauthomanager.infrastructure.dao;

import com.sntk.organauthomanager.domain.model.RoleIdentify;
import com.sntk.organauthomanager.domain.model.RoleResource;
import com.sntk.organauthomanager.domain.repository.RoleResourceRepository;
import com.sntk.organauthomanager.infrastructure.convert.RoleResourceConvert;
import com.sntk.organauthomanager.infrastructure.db.entity.RoleResourceEntity;
import com.sntk.organauthomanager.infrastructure.db.repository.RoleResourceDBRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: RoleResourceRepositoryImpl
 * @author: wgchao
 * @createTime: 2020/7/21 7:34 PM
 * @Description: TODO
 */
@Component
public class RoleResourceRepositoryImpl implements RoleResourceRepository {
    @Autowired
    private RoleResourceDBRepository roleResourceDBRepository;
    @Autowired
    private RoleResourceConvert roleResourceConvert;

    @Override
    public void add(RoleResource roleResource, String operator) {
        //只有新增没有修改 新增前删除原有关系
        List<RoleResourceEntity> roleResourceEntities = roleResourceDBRepository.findAllByRoleCodeAndInstitutionNumber(
                roleResource.getRoleIdentify().getRoleCode(),
                roleResource.getRoleIdentify().getInstitutionNumber());
        if(roleResourceEntities!=null && !roleResourceEntities.isEmpty()){
            roleResourceDBRepository.deleteAll(roleResourceEntities);
            roleResourceDBRepository.findAllByRoleCodeAndInstitutionNumber(roleResource.getRoleIdentify().getRoleCode(),
                    roleResource.getRoleIdentify().getInstitutionNumber());
        }
        roleResourceEntities = roleResourceConvert.convert(roleResource);
        if(roleResourceEntities!=null && !roleResourceEntities.isEmpty()){
            roleResourceEntities.stream().forEach(roleResourceEntity -> {
                roleResourceEntity.setCreateTime(new Date());
                roleResourceEntity.setCreator(operator);
            });
            roleResourceDBRepository.saveAll(roleResourceEntities);
        }
    }

    @Override
    public RoleResource findByKey(RoleIdentify roleIdentify) {
        List<RoleResourceEntity> roleResourceEntities = roleResourceDBRepository.findAllByRoleCodeAndInstitutionNumber(
                roleIdentify.getRoleCode(), roleIdentify.getInstitutionNumber());
        if(roleResourceEntities==null || roleResourceEntities.isEmpty()){
            return null;
        }
        return roleResourceConvert.convert(roleResourceEntities);
    }

    @Override
    public void delete(RoleIdentify roleIdentify, String operator) {
        List<RoleResourceEntity> roleResourceEntities = roleResourceDBRepository.findAllByRoleCodeAndInstitutionNumber(
                roleIdentify.getRoleCode(), roleIdentify.getInstitutionNumber());
        if(roleResourceEntities!=null && !roleResourceEntities.isEmpty()){
            roleResourceDBRepository.deleteAll(roleResourceEntities);
        }
    }

    @Override
    public void removeByRole(RoleIdentify roleIdentify, String operator) {
        roleResourceDBRepository.deleteAllByRoleCodeAndInstitutionNumber(roleIdentify.getRoleCode(), roleIdentify.getInstitutionNumber());
    }

    @Override
    public void removeByResource(String mark, String operator) {
        roleResourceDBRepository.deleteAllByResourceMark(mark);
    }
}
